package com.ruicar.afs.cloud.admin.service.impl;

import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ruicar.afs.cloud.admin.entity.SysPermission;
import com.ruicar.afs.cloud.admin.mapper.SysPermissionMapper;
import com.ruicar.afs.cloud.admin.service.SysPermissionService;
import com.ruicar.afs.cloud.common.core.holder.TransRequestContextHolder;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import java.util.List;

import static com.ruicar.afs.cloud.common.core.constant.CommonConstants.BASE_TENANT_ID;
import static com.ruicar.afs.cloud.common.core.constant.CommonConstants.STATUS_NORMAL;


/**
 * The type SysPermissionServiceImpl.
 * <p>
 * Created on 2019-02-20
 *
 * @author Fzero
 */
@Slf4j
@Service
public class SysPermissionServiceImpl extends ServiceImpl<SysPermissionMapper, SysPermission> implements SysPermissionService {
	/**
	 * Find by level order by sort order list.
	 *
	 * @param level the level
	 * @return the list
	 */
	@Override
	public List<SysPermission> findByLevelOrderBySortOrder(Integer level) {

		return this.list(Wrappers
				.<SysPermission>query().lambda()
				.eq(SysPermission::getLevel, level)
				.eq(!BASE_TENANT_ID.equalsIgnoreCase(TransRequestContextHolder.getTenantId()), SysPermission::getTenantHideFlag,STATUS_NORMAL)
		);
	}

	/**
	 * Find by parent id order by sort order list.
	 *
	 * @param parentId the parent id
	 * @return the list
	 */
	@Override
	public List<SysPermission> findByParentIdOrderBySortOrder(String parentId) {
		return this.list(Wrappers
				.<SysPermission>query().lambda()
				.eq(SysPermission::getParentId, parentId).orderByAsc(SysPermission::getSortOrder)
				.eq(!BASE_TENANT_ID.equalsIgnoreCase(TransRequestContextHolder.getTenantId()), SysPermission::getTenantHideFlag,STATUS_NORMAL)
		);
	}

	/**
	 * Find by type and status order by sort order list.
	 *
	 * @param type   the type
	 * @param status the status
	 * @return the list
	 */
	@Override
	public List<SysPermission> findByTypeAndStatusOrderBySortOrder(Integer type, String status) {
		return this.list(Wrappers
				.<SysPermission>query().lambda()
				.eq(SysPermission::getType, type).eq(SysPermission::getStatus, status).orderByAsc(SysPermission::getSortOrder));
	}

	/**
	 * Find by title list.
	 *
	 * @param title the title
	 * @return the list
	 */
	@Override
	public List<SysPermission> findByTitle(String title) {
		return this.list(Wrappers
				.<SysPermission>query().lambda()
				.eq(SysPermission::getTitle, title).orderByAsc(SysPermission::getSortOrder));
	}

	/**
	 * Find by title like order by sort order list.
	 *
	 * @param title the title
	 * @return the list
	 */
	@Override
	public List<SysPermission> findByTitleLikeOrderBySortOrder(String title) {
		return this.list(Wrappers
				.<SysPermission>query().lambda()
				.like(SysPermission::getTitle, title).orderByAsc(SysPermission::getSortOrder));
	}

	@Override
	public List<SysPermission> findByUserId(Long userId) {
		return baseMapper.findByUserId(userId);
	}
}
